#include <QDir>
#include <QDebug>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QMouseEvent>
#include "OpenedDocsWidget.h"
#include "Include/IconProvider.h"
#include "Include/IEditor.h"


class ListDelegate: public QStyledItemDelegate
{
public:
    ListDelegate(QObject * parent):QStyledItemDelegate(parent){}
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QStyledItemDelegate::paint(painter, option, index);
        if (option.state & QStyle::State_MouseOver)
            IconProvider::instance()->icon("window-close").paint(painter, option.rect.right()-16, option.rect.top(), 16, 16);
        if ((Severity)index.data(Qt::UserRole+1).toInt() != None){
            Severity sev = (Severity)index.data(Qt::UserRole+1).toInt();
            QColor color = (sev == Error ? Qt::red : (sev == Warning ? QColor(200, 200, 0): QColor(0, 0, 200)));
            painter->setBrush(color);
            painter->setPen(color);
            painter->drawRect(option.rect.left(), option.rect.top(), 2, option.rect.height());
        }
    }
};

OpenedDocsWidget::OpenedDocsWidget(QWidget *parent) :
    QListWidget(parent)
{
    setMouseTracking(true);
    setItemDelegate(new ListDelegate(this));
}

OpenedDocsWidget::~OpenedDocsWidget()
{

}

void OpenedDocsWidget::activated(const QString & file)
{
    blockSignals(true);
    if (QListWidgetItem * it = itemByFile(file)){
        setCurrentItem(it);
        scrollToItem(it);
    }
    blockSignals(false);
}

void OpenedDocsWidget::editorOpened(const QString & file)
{
    QDir p(_projectPath);

    QString path = p.relativeFilePath(file);
    if (path.startsWith(".."))
        path = QFileInfo(file).fileName();

    QListWidgetItem *item = new QListWidgetItem("..."+path, this);
    item->setData(Qt::UserRole, file);
}

void OpenedDocsWidget::editorClosed(const QString & file)
{
    if (QListWidgetItem * it = itemByFile(file)){
        removeItemWidget(it);
        delete it;
    }
}

void OpenedDocsWidget::setProjectPath(const QString& path)
{
    _projectPath = path;
    QDir p(_projectPath);
    for(int i = 0; i < count(); ++i){
        QListWidgetItem *it = item(i);
        it->setText(p.relativeFilePath(it->data(Qt::UserRole).toString()));
    }
}

void OpenedDocsWidget::modifyChanged(const QString & file, bool m)
{
    QDir p(_projectPath);
    if (QListWidgetItem * it = itemByFile(file)){
        it->setText(p.relativeFilePath(it->data(Qt::UserRole).toString())+(m ? "*" : ""));
    }
}

QListWidgetItem * OpenedDocsWidget::itemByFile(const QString & file)
{
    for(int i = 0; i < count(); ++i){
        if (item(i)->data(Qt::UserRole).toString() == file){
            return item(i);
        }
    }
    return NULL;
}

void OpenedDocsWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton){
        QListWidgetItem *it = itemAt(event->pos());
        if (it){
            QRect vr = visualItemRect(it);
            if (vr.adjusted(vr.right()-16, 0, 0, 0).contains(event->pos())){
                emit closeEditor(it->data(Qt::UserRole).toString());
                return;
            }
        }
    }
    QListWidget::mousePressEvent(event);
}

void OpenedDocsWidget::currentChanged(const QModelIndex &current, const QModelIndex &/*previous*/)
{
    if (current.isValid())
        emit activateEditor(itemFromIndex(current)->data(Qt::UserRole).toString());
}

void OpenedDocsWidget::severityChanged(const QString & file, int sev)
{
    //qDebug() << itemByFile(file);
    QListWidgetItem *item = itemByFile(file);
    if (item)
        item->setData(Qt::UserRole+1, sev);
    //qDebug() << "set sev" << file << sev;
}
